package com.sec.android.app.myfiles.module.local.recent;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.sec.android.app.myfiles.module.abstraction.AbsReloadStrategyImp;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;

/**
 * Created by daesu83.kim on 2015-09-25.
 */
public class RecentReloadStrategy extends AbsReloadStrategyImp {

    public RecentReloadStrategy(Context context, NavigationInfo navigationInfo) {
        super(context, navigationInfo);
    }

    @Override
    public void reload(ReloadCause cause, Bundle extras, int event) {
        if (cause == ReloadCause.MediaUnmounted) {
            if (extras != null) {
                String unmountedPath = extras.getString(EXTRA_KEY_PATH);
                if (!TextUtils.isEmpty(unmountedPath)) {
                    removeRecentInStorage(unmountedPath);
                }
            }
        }
    }

    private void removeRecentInStorage(final String storagePath) {
        DbTableInfo tableInfo = RecentDbTableInfo.getInstance();
        StringBuilder where = new StringBuilder();
        where.append(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH))
                .append(" LIKE ?");
        String[] whereArgs = new String[]{storagePath + "%"};
        mContext.getContentResolver().delete(Uri.parse(tableInfo.getUri()), where.toString(), whereArgs);
    }
}
